// pages/pond/pond.js
var api = require("../../api.js"), app = getApp(), WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Show:"none",  //遮罩层状态
    result: false,
    last_index: 0, //上一回滚动的位置
    amplification_index: 0, //轮盘的当前滚动位置
    roll_flag: true, //是否允许滚动
    max_number: 8, //轮盘的全部数量
    speed: 350, //速度，速度值越大，则越慢 初始化为300
    finalindex: 5, //最终的奖励
    myInterval: "", //定时器
    max_speed: 50, //滚盘的最大速度
    minturns: 8, //最小的圈数为2
    runs_now: 0, //当前已跑步数
    index: [],
    oppty:[]   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(t) {
    app.pageOnLoad(this), this.loadData(t);
  },

  loadData: function(t) {
    this.reloadGoodsList()
  },

  reloadGoodsList(){
    console.log("===app====")
    console.log(app)
    let that = this
    // 禾匠 奖品列表 data
    app.request({
      url: api.pond.index,
      data: {},
      success: function (res) {
        console.log(res.data.list)
        let list = res.data.list;
        list[0].order = 1
        list[1].order = 2
        list[2].order = 3
        list[3].order = 8
        list[4].order = 4
        list[5].order = 7
        list[6].order = 6
        list[7].order = 5
        list.splice(4, 0, {
          bind: "startrolling",
          order: ""
        });
        for (let x in list) {
          that.setData({
            order: list[x].order
          })
        }
        that.setData({
          index: list,
          oppty: res.data.oppty,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    console.log("========onShow==========>")
    console.log(this.data)
    // this.data.min_height = getApp().globalData.windowheight;
    this.setData(this.data);
  },

  //开始滚动
  startrolling: function (e) {
    console.log("=======startrolling========>")
    console.log(this.data.roll_flag)
    let _this = this;
    let times = _this.data.oppty
    if(times-1 >= 0){
      _this.setData({
        oppty: times - 1
      })
    }else{
      wx.showToast({
        title: '您的次数已用完',
      })
      return false
    }
    //初始化步数
    _this.data.runs_now = 0;
    //当前可以点击的状态下
    if (_this.data.roll_flag) {
      _this.data.roll_flag = false;
      //启动滚盘，注，若是最终后台无返回就不好意思里
      _this.rolling();
    }
  },

  //滚动轮盘的动画效果
  rolling: function (amplification_index) {
    var _this = this;
    this.data.myInterval = setTimeout(function () {
      _this.rolling();
    }, this.data.speed);
    this.data.runs_now++; //已经跑步数加一
    this.data.amplification_index++; //当前的加一
    //获取总步数，接口延迟问题，所以最后还是设置成1s以上
    var count_num = this.data.minturns * this.data.max_number + this.data.finalindex - this.data.last_index;

    //上升期间    
    if (this.data.runs_now <= (count_num / 3) * 2) {
      this.data.speed -= 30; //加速
      if (this.data.speed <= this.data.max_speed) {
        this.data.speed = this.data.max_speed; //最高速度为40；
      }
    }
    
    //抽奖结束
    else if (this.data.runs_now >= count_num) {
      let index = this.data.index
      clearInterval(this.data.myInterval);
      this.data.roll_flag = true;
      console.log("抽奖结束")
      console.log(this.data.roll_flag)
      _this.setData({
        Show: "block"
      })
      app.request({
        url: api.pond.lottery,
        data: {},
        success: function (res) {
          res.data.id === 18 ? _this.setData({
               result: true
          }):
          _this.setData({
               result: false
          })
          _this.setData({
            status: res.data.name
          })
          console.log(res.data)
        }
      })
    }
    //下降期间
    else if (count_num - this.data.runs_now <= 10) {
      this.data.speed += 20;
    }
    //缓冲区间
    else {
      this.data.speed += 10;
      if (this.data.speed >= 100) {
        this.data.speed = 100; //最低速度为100；
      }
    }
    if (this.data.amplification_index > this.data.max_number) { //判定！是否大于最大数
      this.data.amplification_index = 1;
    }
    this.setData(this.data);

  },
 
 close: function(){
    let that = this
    that.setData({
      Show: "none"
    })
 },

clickList: function(){
    wx.navigateTo({
      url: '/pages/pond/list',
    })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})